package com.hjm.project.formalPos.branchPos.controller;

import java.util.List;

import com.hjm.project.finance.agentDeductionsDetails.domain.AgentDeductionsDetails;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.formalPos.branchPos.domain.BranchPos;
import com.hjm.project.formalPos.branchPos.service.IBranchPosService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 手刷机具分配Controller
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/formalPos/branchPos")
public class BranchPosController extends BaseController
{
    private String prefix = "formalPos/branchPos";

    @Autowired
    private IBranchPosService branchPosService;

    @RequiresPermissions("formalPos:branchPos:view")
    @GetMapping()
    public String branchPos()
    {
        return prefix + "/branchPos";
    }

    /**
     * 查询手刷机具分配列表
     */
    @RequiresPermissions("formalPos:branchPos:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BranchPos branchPos)
    {
        startPage();
        List<BranchPos> list = branchPosService.selectBranchPosList(branchPos);
        return getDataTable(list);
    }

    /**
     * 导出手刷机具分配列表
     */
    @RequiresPermissions("formalPos:branchPos:export")
    @Log(title = "手刷机具分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BranchPos branchPos)
    {
        List<BranchPos> list = branchPosService.selectBranchPosList(branchPos);
        ExcelUtil<BranchPos> util = new ExcelUtil<BranchPos>(BranchPos.class);
        return util.exportExcel(list, "branchPos");
    }

    /**
     * 新增手刷机具分配
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存手刷机具分配
     */
    @RequiresPermissions("formalPos:branchPos:add")
    @Log(title = "手刷机具分配", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BranchPos branchPos)
    {
        return toAjax(branchPosService.insertBranchPos(branchPos));
    }

    /**
     * 修改手刷机具分配
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BranchPos branchPos = branchPosService.selectBranchPosById(id);
        mmap.put("branchPos", branchPos);
        return prefix + "/edit";
    }

    /**
     * 修改保存手刷机具分配
     */
    @RequiresPermissions("formalPos:branchPos:edit")
    @Log(title = "手刷机具分配", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BranchPos branchPos)
    {
        return toAjax(branchPosService.updateBranchPos(branchPos));
    }

    /**
     * 删除手刷机具分配
     */
    @RequiresPermissions("formalPos:branchPos:remove")
    @Log(title = "手刷机具分配", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(branchPosService.deleteBranchPosByIds(ids));
    }

    /**
     * 查询服务商申请列表直属下级
     */
    @PostMapping("/selectPos")
    @ResponseBody
    public TableDataInfo selectPos(BranchPos branchPos)
    {
        startPage();
        String[] sns = branchPos.getSns().split(",");
        String sn = "'";
        for(String s :sns){
            sn +=  s + "','";
        }
        sn = sn.substring(0,sn.length()-2);
        branchPos.setSns(sn);
        List<AgentDeductionsDetails> agentDeductionsDetails = branchPosService.selectFeignActivate(branchPos);
        return getDataTable(agentDeductionsDetails);
    }
}
